package com.example.demo.common;

/**
 * @author lai
 * @data 2021/11/16
 * @apiNote
 */
public class Result<T> {

    private String code; ////后端返回结果正常为0，发生异常返回非0
    private String msg; //后端返回的信息
    private T data; //后端返回结果的数据对象

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Result() {
    }

    public Result(T data) {
        this.data = data;
    }

    /**
     * 返回的一些常用方法
     */
    public static Result success() {
        Result result = new Result<>();
        result.setCode("0");
        result.setMsg("成功");
        return result;
    }

    public static Result fail() {
        Result result = new Result<>();
        result.setCode("1");
        result.setMsg("失败");
        return result;
    }

    public static <T> Result<T> success(T data) {
        Result result = new Result<>(data);
        result.setCode("0");
        result.setMsg("成功");
        return result;
    }

    public static Result error(String code, String msg) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }
}
